package com.sk.commonadapter_android.adapter;

import android.content.Context;
import android.content.pm.ProviderInfo;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

/**
 * @Author yemao
 * @Email yrmao9893@163.com
 * @Date 2017/7/5
 * @Des RecyclerView包裹adapter用于添加headerView，footerView 下拉刷新，上拉加载!
 */

public class RecyclerViewWrapAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_REFRESH_HEADER = 1;//刷新的headerView
    private static final int TYPE_HEADER = 2;//普通的headerView
    private static final int TYPE_NOMAL = 3;//正常的itemView
    private static final int TYPE_FOOTER = 4;

    private RecyclerView.Adapter mAdapter;
    private SparseArray<View> mHeaderViews;
    private SparseArray<View> mFooterViews;
    private int headerPosition=1;

    public RecyclerViewWrapAdapter(RecyclerView.Adapter adapter, SparseArray<View> headerViews, SparseArray<View> footerViews) {
        this.mAdapter = adapter;
        this.mHeaderViews = headerViews;
        this.mFooterViews = footerViews;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager layoutManager=recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager){
            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (isRefreshHeader(position)||isHeader(position)||isFooter(position)?((GridLayoutManager) layoutManager).getSpanCount():1);
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams params=holder.itemView.getLayoutParams();
        if (params instanceof StaggeredGridLayoutManager.LayoutParams
                && (isRefreshHeader(holder.getLayoutPosition())||isHeader(holder.getLayoutPosition())||isFooter(holder.getLayoutPosition()))){
            StaggeredGridLayoutManager.LayoutParams staggeredGridLayoutManager= (StaggeredGridLayoutManager.LayoutParams) params;
            staggeredGridLayoutManager.setFullSpan(true);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_HEADER) {

            return  new RecyclerView.ViewHolder(mHeaderViews.get(headerPosition++)) {};
        } else if (viewType == TYPE_REFRESH_HEADER) {

            return new RecyclerView.ViewHolder(mHeaderViews.get(0)) {};

        } else if (viewType == TYPE_FOOTER) {
            return new RecyclerView.ViewHolder(mFooterViews.get(0)) {};

        }
        return mAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemViewType(int position) {
        if (isRefreshHeader(position)){
            return TYPE_REFRESH_HEADER;
        }
        if (isHeader(position)){
            return TYPE_HEADER;
        }
        if (isFooter(position)){
            return TYPE_FOOTER;
        }
        int adjPosition=position-getHeadersCount();
        int adapterConunt;
        if (mAdapter!=null){
            adapterConunt=mAdapter.getItemCount();
            if (adjPosition<adapterConunt){
                return mAdapter.getItemViewType(position);
            }
        }
        return TYPE_NOMAL;
    }

    @Override
    public int getItemCount() {
        if (mAdapter!=null){
            return getHeadersCount()+getFootersCount()+mAdapter.getItemCount();
        }else {
            return getHeadersCount()+getFootersCount();
        }
    }
    public int getHeadersCount(){
        return mHeaderViews.size();
    }
    public int getFootersCount(){
        return mFooterViews.size();
    }
    public boolean isHeader(int position){
        return  position>0&&position<mHeaderViews.size();
    }
    public boolean isRefreshHeader(int positon){
        return positon==0&&mHeaderViews.size()>0;
    }
    public boolean isFooter(int position){
        return position<getItemCount()&&position>=getItemCount()-mFooterViews.size();
    }
    @Override
    public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        if (mAdapter != null) {
            mAdapter.unregisterAdapterDataObserver(observer);
        }
    }

    @Override
    public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        if (mAdapter != null) {
            mAdapter.registerAdapterDataObserver(observer);
        }
    }

}
